package com.party.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.party.business.domain.TBusinessDept;
import com.party.business.domain.TBusinessPost;
import com.party.business.domain.TKs;
import com.party.business.domain.TKsQuestion;
import com.party.business.service.ITBusinessDeptService;
import com.party.business.service.ITBusinessPostService;
import com.party.business.service.ITKsQuestionService;
import com.party.business.service.ITKsService;
import com.party.common.annotation.Log;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.utils.StringUtils;
import com.party.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 试卷Controller
 *
 * @author ruoyi
 * @date 2024-11-09
 */
@RestController
@RequestMapping("/business/ks" )
public class TKsController extends BaseController {
    @Autowired
    private ITKsService tKsService;
    @Autowired
    private ITKsQuestionService ksQuestionService;

    @Autowired
    private ITBusinessPostService businessPostService;

    @Autowired
    private ITBusinessDeptService businessDeptService;



/**
 * 查询试卷列表
 */
//@RequiresPermissions("system:ks:list")
@GetMapping("/list" )
    public TableDataInfo list(TKs tKs) {
        startPage();
        List<TKs> list = tKsService.selectTKsList(tKs);
        return getDataTable(list);
    }



    /**
     * 导出试卷列表
     */
    //@RequiresPermissions("system:ks:export")
    @Log(title = "试卷" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TKs tKs) {
        List<TKs> list = tKsService.selectTKsList(tKs);
        ExcelUtil<TKs> util = new ExcelUtil<TKs>(TKs. class);
        util.exportExcel(response, list, "试卷数据" );
    }

    /**
     * 获取试卷详细信息
     */
    //@RequiresPermissions("system:ks:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(tKsService.selectTKsById(id));
    }

    /**
     * 新增试卷
     */
    //@RequiresPermissions("system:ks:add")
    @Log(title = "试卷" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TKs tKs) throws Exception {
        boolean save = tKsService.save(tKs);
        List<TKsQuestion> quList = tKs.getQuList();
        if(CollectionUtil.isEmpty(quList)){
            throw  new Exception("请选择题目");
        }
        // 保存试题
        for (int i = 0; i < quList.size(); i++) {
            quList.get(i).setKsId(tKs.getId());
            ksQuestionService.save(quList.get(i));
        }
          tKsService.updatePostAndDept(tKs);
        return  AjaxResult.success();
    }

    /**
     * 修改试卷
     */
    //@RequiresPermissions("system:ks:edit")
    @Log(title = "试卷" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TKs tKs) throws Exception {
        boolean b = tKsService.updateById(tKs);
        List<TKsQuestion> quList = tKs.getQuList();
        if(CollectionUtil.isEmpty(quList)){
            throw  new Exception("请选择题目");
        }
        //
        TKsQuestion question=new TKsQuestion();
        question.setKsId(tKs.getId());
        List<TKsQuestion>  oldQuestions = ksQuestionService.selectTKsQuestionList(question);
        // 讲新数据不存在的老数据 删除
        for (int i = 0; i < oldQuestions.size(); i++) {
            String id = oldQuestions.get(i).getId();
            boolean b1 = quList.stream().anyMatch(item -> id.equals(item.getId()));
            if(!b1){
                ksQuestionService.removeById(oldQuestions.get(i).getId());
            }
        }
        for (int i = 0; i < quList.size(); i++) {
            quList.get(i).setKsId(tKs.getId());
            ksQuestionService.saveOrUpdate(quList.get(i));
        }
        // 处理岗位和部门
        tKsService.updatePostAndDept(tKs);
        return success();
    }

    /**
     * 删除试卷
     */
    //@RequiresPermissions("system:ks:remove")
    @Log(title = "试卷" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tKsService.removeByIds(Arrays.asList(ids)));
    }
}
